package com.kangaroo.myhome.service.impl;

import java.lang.reflect.Method;
import java.util.*;

import com.kangaroo.myhome.mapper.TInspProjectMapper;
import com.kangaroo.myhome.mapper.TInspProjectPlanMapper;
import com.kangaroo.myhome.model.*;
import com.kangaroo.myhome.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TInspProjectMapper;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class TInspProjectService {

    @Autowired
    private TInspProjectMapper tInspProjectMapper;

    @Autowired
    private TInspProjectPlanMapper tInspProjectPlanMapper;

    /**
     * @param subway
     * @param cat
     * @param degree
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String subway, String cat, String degree,String stage,String name, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString1(subway);
        user.setString2(cat);
        user.setString3(degree);
        user.setString4(stage);
        user.setString5(name);
        user.setPagination(page, rows);

        List<TInspProject> users = tInspProjectMapper.selectByPagination(user);
        long count = tInspProjectMapper.selectByPaginationCount(user);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }


    /**
     * @param subway
     * @return
     */
    public ResponseModel findReportData(String subway) {
        ResponseModel model = new ResponseModel();

        CommonSearch search = new CommonSearch();
        search.setString1(subway);

        List<Map> tInspProjects = tInspProjectMapper.findReportData(search);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setData(tInspProjects);
        return model;
    }

    public ResponseModel findAllJson(String subway, String cat, String degree,String stage,String name, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString1(subway);
        user.setString2(cat);
        user.setString3(degree);
        user.setString4(stage);
        user.setString5(name);
        user.setPagination(page, rows);

        List<TInspProject> users = tInspProjectMapper.selectByPaginationJson(user);
        long count = tInspProjectMapper.selectByPaginationJsonCount(user);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    public List<TInspProject> findAllExcel(String subway, String cat, String degree,String stage) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString1(subway);
        user.setString2(cat);
        user.setString3(degree);
        user.setString4(stage);
        user.setPagination(1, 999999);

        List<TInspProject> users = tInspProjectMapper.findAllExcel(user);
        return users;
    }

    /**
     * @param tInspProject
     * @return TInspProject tInspProject
     */
    public ResponseModel edit(TInspProject tInspProject) {

        ResponseModel model = new ResponseModel();

        if (tInspProject == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tInspProject.getId();
        if (StringUtil.isEmpty(id)) {
            TInspProject subWayExist = view(tInspProject);
            if (subWayExist != null) {
                model.setCode(201);
                model.setMessage("项目编码：【" + tInspProject.getCode() + "】已经存在！");
                return model;
            } else {
                tInspProject.setCreateTime(new Date());
                tInspProject.setId(UUID.randomUUID().toString());
                // tInspProject.setStage("stageStart");
                tInspProject.setCreator(SecurityUtils.getUsername());
                result = tInspProjectMapper.insertSelective(tInspProject);
            }

        } else {
            // 编辑的阶段
            String stage = tInspProject.getStage();
            result = tInspProjectMapper.updateByPrimaryKeySelective(tInspProject);
            /*if ("stageStart".equals(stage)) {
                tInspProject.setStage(null);
                result = tInspProjectMapper.updateByPrimaryKeySelective(tInspProject);
            } else {
                tInspProject.setStage(null);
                TInspProject project = new TInspProject();
                project.setId(tInspProject.getId());
                if ("stageTdcr".equals(stage)) {
                    project.setStageTdcr(tInspProject.getStageTdcr());
                } else if ("stagePlan".equals(stage)) {
                    project.setStagePlan(tInspProject.getStagePlan());
                } else if ("stageDesign".equals(stage)) {
                    project.setStageDesign(tInspProject.getStageDesign());
                } else if ("stageCons".equals(stage)) {
                    project.setStageCons(tInspProject.getStageCons());
                } else if ("stageTracing".equals(stage)) {
                    project.setStageTracing(tInspProject.getStageTracing());
                }
                result = tInspProjectMapper.updateByPrimaryKeySelective(project);
            }*/

        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }


    public ResponseModel editInfo(TInspProject tInspProject) {

        ResponseModel model = new ResponseModel();

        if (tInspProject == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tInspProject.getId();
        if (StringUtil.isEmpty(id)) {
            model.setCode(201);
            model.setMessage("ID为空不可编辑");
            return model;
        } else {
            // 编辑的阶段
            String stage = tInspProject.getStage();
            if ("stageStart".equals(stage)) {
                tInspProject.setStage(null);
                result = tInspProjectMapper.updateByPrimaryKeySelective(tInspProject);
            } else {
                tInspProject.setStage(null);
                TInspProject project = new TInspProject();
                project.setId(tInspProject.getId());
                if ("stageTdcr".equals(stage)) {
                    project.setStageTdcr(tInspProject.getStageTdcr());
                } else if ("stagePlan".equals(stage)) {
                    project.setStagePlan(tInspProject.getStagePlan());
                } else if ("stageDesign".equals(stage)) {
                    project.setStageDesign(tInspProject.getStageDesign());
                } else if ("stageCons".equals(stage)) {
                    project.setStageCons(tInspProject.getStageCons());
                } else if ("stageTracing".equals(stage)) {
                    project.setStageTracing(tInspProject.getStageTracing());
                }
                result = tInspProjectMapper.updateByPrimaryKeySelective(project);
            }

        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tInspProject
     * @return
     */
    public TInspProject view(TInspProject tInspProject) {
        ResponseModel model = new ResponseModel();

        TInspProjectExample example = new TInspProjectExample();
        String id = tInspProject.getId();
        String code = tInspProject.getCode();
        TInspProject resultSubWay = null;
        if (StringUtil.isNotEmpty(id)) {
            resultSubWay = tInspProjectMapper.selectByPrimaryKey(id);
            TInspProjectPlanExample ex = new TInspProjectPlanExample();
            ex.createCriteria().andProjectIdEqualTo(resultSubWay.getId());
            ex.setOrderByClause("create_time desc");
            List<TInspProjectPlan> plans = tInspProjectPlanMapper.selectByExample(ex);
            if(plans!=null &&plans.size()>0){
                List<TInspProjectPlan> pp = new ArrayList<TInspProjectPlan>();
                pp.add(plans.get(0));
                resultSubWay.setPlans(pp);
            }
            return resultSubWay;
        }
        if (StringUtil.isNotEmpty(code)) {
            example.createCriteria().andCodeEqualTo(code);
            List<TInspProject> users = tInspProjectMapper.selectByExample(example);
            if (users != null && users.size() > 0) {
                resultSubWay = users.get(0);
                TInspProjectPlanExample ex = new TInspProjectPlanExample();
                ex.createCriteria().andProjectIdEqualTo(resultSubWay.getId());
                ex.setOrderByClause("create_time desc");
                List<TInspProjectPlan> plans = tInspProjectPlanMapper.selectByExample(ex);
                if(plans!=null &&plans.size()>0){
                    List<TInspProjectPlan> pp = new ArrayList<TInspProjectPlan>();
                    pp.add(plans.get(0));
                    resultSubWay.setPlans(pp);
                }
            }
        }

        return resultSubWay;
    }

    public void delete(TInspProject tInspProject) {
        String ids = tInspProject.getId();
        String[] idArr = ids.split(",");
        for(String id : idArr){
            tInspProjectMapper.deleteByPrimaryKey(id);

        }
    }

}
